package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC413 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int numberOfArithmeticSlices(int[] arr) {
            if (null == arr || arr.length < 3) return 0;
            int count = 0, preDiff = 0, afDiff = 1, lastConsecutivePos = 0, i = 2;
            for (i = 2; i < arr.length; i++) {
                preDiff = arr[i - 1] - arr[i - 2];
                afDiff = arr[i - 0] - arr[i - 1];
                if (preDiff == afDiff) continue;

                count += (i - lastConsecutivePos - 1) * (i - lastConsecutivePos - 2) / 2;
                lastConsecutivePos = i - 1;
            }

            if (preDiff == afDiff) {
                count += (i - lastConsecutivePos - 1) * (i - lastConsecutivePos - 2) / 2;
            }

            lastConsecutivePos = i - 1;

            return count;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3,4]");
        System.out.println(solution.numberOfArithmeticSlices(arr));
    }

    public static void main(String[] args) throws Exception {
        LC413 an = new LC413();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}



